//
//  SCBaseCollectionViewController.m
//  Shuaiche
//
//  Created by dangshuai on 2019/3/11.
//  Copyright © 2019 zzs. All rights reserved.
//

#import "SCBaseCollectionViewController.h"
#import <MJRefresh.h>
#import "SCPlacehodlerView.h"


@interface SCBaseCollectionViewController ()
@property (nonatomic, strong) UIView *noDataView;
@end

@implementation SCBaseCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _pageNo = 1;
    _pageSize = 20;
    [self createCollectionView];
}

- (void)createCollectionView {
    
    _collectionView = [[UICollectionView alloc] initWithFrame:[self collectionViewFrame] collectionViewLayout:[self collectionViewLayout]];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = SC_Color_BG;
    _collectionView.alwaysBounceVertical = YES;
    [self.view addSubview:_collectionView];
    [self configureCollectionView];
    if (@available(iOS 11.0 , *)) {
        _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}

- (void)configureCollectionView{}

- (void)registerCellClass:(Class)cell {
    [_collectionView registerClass:cell forCellWithReuseIdentifier:NSStringFromClass(cell)];
}

- (void)registerNibCellName:(NSString *)cell {
    [_collectionView registerNib:[UINib nibWithNibName:cell bundle:nil] forCellWithReuseIdentifier: cell];
}

- (void)registerNibHeaderName:(NSString *)header {
    [_collectionView registerNib:[UINib nibWithNibName:header bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:header];
}

- (CGRect)collectionViewFrame {
    return CGRectMake(0, 0, kSCScreenWidth, kSCScreenHeight);
}

- (UICollectionViewLayout *)collectionViewLayout {
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    return flow;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                           cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}


#pragma mark -- Data Fresh

- (void)addGifHeaderRefresh {
   
    [self addGifHeaderAutoRefresh:NO];
}

- (void)addGifHeaderAutoRefresh:(BOOL)refresh {
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRequestData)];
    NSArray *array = [self refreshImages];
    [header setImages:array forState:MJRefreshStateRefreshing];
    [header setImages:array forState:MJRefreshStateIdle];
    [header setImages:array forState:MJRefreshStatePulling];
    header.lastUpdatedTimeLabel.hidden = YES;
    _collectionView.mj_header = header;
    
    if (refresh) {
        [_collectionView.mj_header beginRefreshing];
    }
}

- (void)addHeaderRefresh {
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRequestData)];
    header.lastUpdatedTimeLabel.hidden = YES;
    [header setTitle:@"" forState:MJRefreshStateIdle];
    [header setTitle:@"" forState:MJRefreshStatePulling];
    header.stateLabel.hidden = YES;
    header.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    header.arrowView.image = [UIImage new];
    _collectionView.mj_header = header;
}

- (void)hideHeaderRefreshView:(BOOL)hide {
    _collectionView.mj_header.hidden = hide;
}

- (void)removeHeaderRefrehs {
    self.collectionView.mj_header = nil;
}

- (void)addFooterRefresh {
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerLoadMoreData)];
    footer.stateLabel.textColor = SCColor(0x999999);
    [footer setTitle:@"" forState:MJRefreshStateNoMoreData];
    [footer setTitle:@"正在加载..." forState:MJRefreshStateRefreshing];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    _collectionView.mj_footer = footer;
}

- (void)headerRequestData {
    _pageNo = 1;
    [_collectionView.mj_footer resetNoMoreData];
    [self pullRequestData];
}

- (void)footerLoadMoreData {
    _pageNo++;
    [self loadMoreData];
}

- (void)pullRequestData{}
- (void)loadMoreData{}

- (void)endRefresh {
    [_collectionView.mj_header endRefreshing];
    [_collectionView.mj_footer endRefreshing];
}

- (void)endRefreshWithNoMoreData:(BOOL)noMore {
    [_collectionView.mj_header endRefreshing];
    if (noMore) {
        [_collectionView.mj_footer endRefreshingWithNoMoreData];
        [_collectionView.mj_footer setHidden:NO];
    } else {
        [_collectionView.mj_footer endRefreshing];
    }
}

- (void)noMoreData {
    [_collectionView.mj_footer endRefreshingWithNoMoreData];
    [_collectionView.mj_footer setHidden:NO];
}

- (void)reloadCollectionView {
    [_collectionView reloadData];
    if (_dataSource.count) {
        _collectionView.backgroundColor = SC_Color_BG;
        _noDataView.hidden = YES;
        _collectionView.scrollEnabled = YES;
    } else {
        _collectionView.backgroundColor = [UIColor whiteColor];
        self.noDataView.hidden = NO;
        _collectionView.scrollEnabled = NO;
    }
}

#pragma mark -- GET

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}

- (NSMutableDictionary *)params {
    if (!_params) {
        _params = [NSMutableDictionary dictionaryWithCapacity:0];
    }
    return _params;
}


- (NSMutableArray *)refreshImages {
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    for (int i = 1; i<=10; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"refresh_%d", i]];
        [array addObject:image];
    }
    return array;
}

- (UIView *)noDataView {
    if (!_noDataView) {
        
        if (![self emptyImageName]) {
            return nil;
        }
        
        _noDataView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCScreenWidth, CGRectGetHeight(self.view.bounds))];
        _noDataView.backgroundColor = [UIColor whiteColor];
        self.collectionView.backgroundView = _noDataView;
        
        SCPlacehodlerView *view = [[SCPlacehodlerView alloc] init];
        [_noDataView addSubview: view];
        view.imageName = [self emptyImageName];
        view.message = [self emptyTipString];
        view.buttonTitle = [self emptyButtonTitle];
        view.eventInvocation = [self buttonInvocation];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@15);
            make.right.equalTo(@(-15));
            make.centerY.equalTo(_noDataView.mas_centerY).offset(-kSCNavigationHeight/2);
        }];
    }
    return _noDataView;
}

- (NSInvocation *)buttonInvocation {
    if (self.buttonEventName) {
        SEL sel = NSSelectorFromString(self.buttonEventName);
        NSMethodSignature *signature = [self methodSignatureForSelector:sel];
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
        invocation.target = self;
        invocation.selector = sel;
        return invocation;
    }
    return nil;
}

- (NSString *)emptyImageName {
    return @"emp_group";
//    return @"emp_default";
}

- (NSString *)emptyTipString {
    return @"暂无数据";
}

- (NSString *)emptyButtonTitle {
    return nil;
}

@end
